// pages/Search/addinfo/addinfo.js
var app = getApp();
var ikz = require('../../Common/tools.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mapchoose:'定位',//选择定位的按钮文字
    mapbtn:'',//地图选择按钮背景色
    latitude:'',//纬度
    longitude: '',//经度
    town_info: '',//所有镇的信息
    type_info: '',//所有信息分类
    typindex: 0,//分类选项下标
    twindex: 0,//地区的下标
    twid:'',//选择的城镇id
    tpid:'',//所选分类id
    twname: '',//选择的城镇name
    tpname: '',//所选分类name
  },
  //获取分类id
  bindPickerChange:function(e){
    var type_info = this.data.type_info;
    for(var i = 0; i < type_info.length; i++){
      if (i == e.detail.value){
        var tpid = type_info[i]['typ_id'];
        var tpname = type_info[i]['typ_name'];
      }
    }
    console.log(tpid);
    this.setData({
      typindex: e.detail.value,
      tpid: tpid,
      tpname: tpname,
    })
  },
  //获取地区id
  bindMultiPickerChange: function (e) {
    var town_info = this.data.town_info;
    for(var i = 0; i < town_info.length; i++){
      if (e.detail.value == i){
        var twid = town_info[i]['town_id'];
        var twname = town_info[i]['town_name'];
      }
    }
    
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      twindex: e.detail.value,
      twid: twid,
      twname: twname,
    })
  },
  
  //选择定位，获取经纬度
  chooseMap:function(e){
    var that = this;
    wx.authorize({
      scope: 'scope.userLocation',
      success() {
        wx.chooseLocation({
          success: function (res) {
            that.setData({
              latitude: res.latitude,
              longitude: res.longitude,
              mapchoose: '已定位',
              mapbtn: 'red',
            });
          },
          complete: function (res) {
            console.log(res);
            console.log(165465);
          },
        });
      }
    })
    
    
  },

  //提交表单
  formSubmit:function(e){
    console.log(e);
    var that = this;
    var shopname = e.detail.value.shopname;
    var shopphone = e.detail.value.shopphone;
    var shopaddress = e.detail.value.shopaddress;
    var latitude = that.data.latitude;
    var longitude = that.data.longitude;
    var tpid = that.data.tpid;
    var twid = that.data.twid;
    var tpname = that.data.tpname;
    var twname = that.data.twname;
    var data = {
      name: shopname,
      phone: shopphone,
      address: shopaddress,
      longitude: longitude,
      latitude: latitude,
      tpid: tpid,
      twid: twid,
      tpname: tpname,
      twname: twname,
    };
    console.log(data);
    console.log(3434);
    var path = 'Search/addInfo';
    ikz.request(path, data, true, (res) => {
      if (res.data.status == 'success') {
        wx.showModal({
          title: '提示',
          content: '信息已发布，请等待管理员审核！',
          showCancel: false
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    ikz.over(that);
    var path = 'Search/allInfo';
    ikz.request(path, {}, false, (res) => {
      if (res.data.status == 'success') {
        //获取默认选中的城镇id
        if (res.data.town_info) {
          var twid = res.data.town_info[0].town_id;
          var twname = res.data.town_info[0].town_name;
        }
        //获取默认选中的分类id
        if (res.data.town_info) {
          var tpid = res.data.type_info[0].typ_id;
          var tpname = res.data.type_info[0].typ_name;
        }
        
        that.setData({
          town_info: res.data.town_info,
          type_info: res.data.type_info,
          twid: twid,
          tpid: tpid,
          twname: twname,
          tpname: tpname,
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})